#! /usr/local/bin/python
#-*- coding: utf-8 -*-

"""Calcul des statistiques des scores.
utilise le module Python pylab.
"""

"""
Minesweeper 3K. Original implementation of the classic game of mines
for Python 3.
Copyright (C) 2010  Cedric Bonhomme

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

Minesweeper 3K  Copyright (C) 2010  Cedric Bonhomme
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
"""

__author__ = "Jerome Hussenet, Cedric Bonhomme"
__date__ = "$Date: 2010/02/27 $"
__copyright__ = "Copyright (c) 2010 Jerome Hussenet, Copyright (c) 2010 Cedric Bonhomme"
__license__ = "GPL 3"

import tkinter.messagebox

import score
import math

def trace(niveau):
    """Affiche l'histogramme.

    Les scores sont récupérés via la classe Score du module score.py.
    """
    try:
        import pylab
    except ImportError:
        print(_("Erreur : module pylab manquant."))
        print("http://matplotlib.sourceforge.net/")
        return

    scores = score.Score()
    liste = scores.getPersonnes(niveau)

    # le dictionnaire dic contient en clé les prenoms et en valeurs
    # un tuple : (somme des scores du joueur, nombre de parties jouées)
    if liste:
        dic = {}
        for elem in liste:
            dic[elem.prenom] = (0,  [elem1.prenom for elem1 in liste].count(elem.prenom))
        for elem in liste:
            dic[elem.prenom] = (dic[elem.prenom][0] + int(elem.temps), dic[elem.prenom][1])
        for elem in dic:
            dic[elem] = (round((dic[elem][0] * 1.0) / dic[elem][1], 2), dic[elem][1])

        length = len(dic)
        ind = pylab.arange(length)  # abscisse des personnes
        width = 0.35       # largeur des barres

        scorePers = [leTuple[0] for leTuple in list(dic.values())] # tuple des scores moyens
        namePers = list(dic.keys())  # tuple des prenoms

        for count, elem in enumerate(namePers):
            namePers[count] = elem + '\nMoyenne : '+ str(dic[elem][0]) + \
                             '\n ' +str(dic[elem][1])  + ' partie(s)'

        maxScore = max(scorePers)  # score moyen le plus mauvais

        p = pylab.bar(ind, scorePers, width, color='r')

        pylab.ylabel(_('Scores'))
        pylab.title(_('Scores par personne pour le niveau ' + niveau))
        pylab.xticks(ind + (width / 2), namePers)
        pylab.xlim(-width, len(ind))
        pylab.ylim(0, maxScore + 5)
        pylab.yticks(pylab.arange(0, maxScore + 5, 5))

        pylab.show()
    else:
        print(_("Aucun score pour le niveau"), niveau)
        tkinter.messagebox.showerror('Erreur',\
                _("Aucun score pour le niveau " + niveau))

if __name__ == '__main__':
    # Point d'entrée en mode exécution.
    import gettext, os, sys
    pathname = os.path.dirname(sys.argv[0])
    localdir = os.path.abspath(pathname) + "/locale"
    gettext.install("messages", localdir, str=False)
    trace('debutant')